package com.jinxin.platform.cdysylives.controller;

import com.jinxin.platform.base.api.annotation.APCOSApi;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdysylives.bean.CdysylivesSecretkey;
import com.jinxin.platform.cdysylives.rpc.CdysyliveRpc;
import com.jinxin.platform.cdysylives.service.ICdysylivesSecretkeyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author 那个男人
 * @Date 2023/7/25 14:50
 * @Version 1.0
 */
@APCOSApi(value = "秘钥相关接口", developers = {"那个男人"}, description = "秘钥相关接口")
@RestController
@RequestMapping("/cdysylives/secret")
@Slf4j
public class Cdys7liveSecretController {
    @Autowired
    private ICdysylivesSecretkeyService secretkeyService;
    @Autowired
    private CdysyliveRpc cdysyliveRpc;

    @APCOSApiOperation(value = "保存或更新应用秘钥信息", developers = {"lingnan"}, description = "保存或更新应用秘钥信息",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody CdysylivesSecretkey record){
        return secretkeyService.saveOrUpdate(record);
    }

    @APCOSApiOperation(value = "分页查询秘钥", developers = {"lingnan"}, description = "分页查询秘钥",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/list")
    public Map<String,Object> list(@RequestParam int page, @RequestParam int limit,
                                   @RequestParam(required = false,name = "appKey")String appKey,
                                   @RequestParam(required = false,name = "isEnabled")Integer isEnabled){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("appKey", appKey);
        param.put("isEnabled", isEnabled);
        Set<String> orgNameSet = cdysyliveRpc.getOrgNameSet();
        if(orgNameSet.size() == 0){
            resultMap.put("count", 0);
            resultMap.put("data", null);
            return resultMap;
        }
        param.put("orgNameSet",orgNameSet);
        resultMap.put("count", secretkeyService.listPageCount(param));
        resultMap.put("data", secretkeyService.listPage(param));
        return resultMap;
    }

    @APCOSApiOperation(value = "删除秘钥", developers = {"lingnan"}, description = "删除秘钥",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/delete")
    public JsonResult delete(String id){
        return secretkeyService.delete(id);
    }

    @APCOSApiOperation(value = "批量启用秘钥", developers = {"lingnan"}, description = "批量启用秘钥",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/batchEnable")
    public JsonResult batchEnable(@RequestBody List<String> idList){
        return secretkeyService.batchEnable(idList);
    }

    @APCOSApiOperation(value = "批量禁用秘钥", developers = {"lingnan"}, description = "批量禁用秘钥",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/batchDisable")
    public JsonResult batchDisable(@RequestBody List<String> idList){
        return secretkeyService.batchDisable(idList);
    }

    @APCOSApiOperation(value = "查询社区列表", developers = {"lingnan"}, description = "查询社区列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/queryCommunityList")
    public JsonResult queryCommunityList(){
        return secretkeyService.queryCommunityList();
    }

}
